package com.web.manager;

import com.web.dao.KftApplyMapper;
import com.web.dao.StoreApplyMapper;
import com.web.domain.ApiResponse;
import com.web.dto.StoreApplyDto;
import com.web.entity.KftApply;
import com.web.entity.StoreApply;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 *
 * @author FDjavaone
 * @date 2018/7/11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoreApplyManager {
    @Autowired
    private StoreApplyMapper storeApplyMapper;
    @Autowired
    private KftApplyMapper kftApplyMapper;

    public ApiResponse apply(StoreApplyDto storeApplyDto) throws Exception {
        Date date=new Date();
        StoreApply storeApply=new StoreApply();
        storeApply.setCreateTime(date);
        storeApply.setModifyTime(date);
        BeanUtils.copyProperties(storeApplyDto,storeApply);
        KftApply old=kftApplyMapper.findByMid(storeApplyDto.getMId());
        if(old!=null){
            BeanUtils.copyProperties(storeApplyDto,old);
            old.setName(storeApplyDto.getContactor());
            old.setPhone(storeApplyDto.getContactPhone());
            old.setStatus(1);
            kftApplyMapper.updateByPrimaryKeySelective(old);
        }else{
            KftApply kftApply=new KftApply();
            BeanUtils.copyProperties(storeApplyDto,kftApply);
            kftApply.setName(storeApplyDto.getContactor());
            kftApply.setPhone(storeApplyDto.getContactPhone());
            kftApplyMapper.insertSelective(kftApply);
        }
        storeApplyMapper.insertSelective(storeApply);
        return ApiResponse.custom().ok("申请提交成功！");
    }


    public ApiResponse applyEditNew(StoreApplyDto storeApplyDto) throws Exception {
        Date date=new Date();
        StoreApply storeApply=new StoreApply();
        storeApply.setModifyTime(date);
        BeanUtils.copyProperties(storeApplyDto,storeApply);
        storeApply.setStatus(0);
        KftApply old=kftApplyMapper.findByMid(storeApplyDto.getMId());
        KftApply kftApply=new KftApply();
        kftApply.setModifyTime(date);
        BeanUtils.copyProperties(storeApplyDto,kftApply);
        kftApply.setStatus(1);
        kftApply.setName(storeApplyDto.getContactor());
        kftApply.setPhone(storeApplyDto.getContactor());
        kftApply.setId(old.getId());
        storeApplyMapper.updateByPrimaryKeySelective(storeApply);
        kftApplyMapper.updateByPrimaryKeySelective(kftApply);
        return ApiResponse.custom().ok("申请重新提交成功！");
    }



}
